package scripts;
import javax.swing.JOptionPane;
import bot.ScriptMethods;

public class MultiSmither extends ScriptMethods implements Runnable {

	enum barEnum {
		bronze, iron, steel, mithril, adamantite, runite
	};

	public void start() {
		System.gc();
		getAccountSettings();

		scriptRunning = true;
		scriptThread = new Thread(this);
		scriptThread.start();
	}

	public void stop() {
		System.gc();
		scriptRunning = false;
		scriptThread = null;
	}

	public void run() {

		Object[] options = { "Bronze", "Iron", "Steel", "Mithril",
				"Adamantite", "Runite" };
		Object[] options2 = { "Dagger", "Large Helmet", "Kite Shield",
				"2-handed sword", "Plated Skirt", "Plate mail legs",
				"Plate Mail Body" };

		String barType = (String) JOptionPane.showInputDialog(null, "",
				"What type of metal?", JOptionPane.PLAIN_MESSAGE, null,
				options, options[5]);
		
		String smithItem = (String) JOptionPane.showInputDialog(null, "",
				"Item to smith?", JOptionPane.PLAIN_MESSAGE, null, options2,
				options2[6]);
		
		int[] itemArray = new int[9];
		itemArray = (checkBarType(barType));

		while (scriptRunning) {
			try {
				if (!isSleeping() && invContains(itemArray[0])) {
					doSmithing(itemArray[0], itemArray[itemArray[8]], smithItem);
				}
				if (!invContains(itemArray[0]) && !isSleeping()) {
					doBank(itemArray[0], itemArray[itemArray[8]]);
				}

				handleEvents();
				wait(random(50, 100));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void handleEvents() throws Exception {
		if (welcomeBoxOpen())
			setWelcomeBox();
		if (!isLoggedIn())
			doLogin();
		if (getFatigue() > 80 && !isSleeping()) {
			wait(600);
			useItem(1263);
			wait(600);
		}
		if (getFatigueSleeping() < 10 && isSleeping())
			doSleeping();
		if (getFightMode() != fightMode)
			setFightMode();
	}

	public void doBank(int barID, int itemID) {
		try {
			if (ourPlayerY() < 507) {
				if (bankOpen()) {
					if (invContains(itemID)) {
						depositAll(itemID);
						wait(600);
					} else {
						withdraw(barID, 25);
						wait(600);
						closeBank();
					}
				} else {
					openBank();
					wait(2000);
				}
			} else {
				int[] bankDoor = getObjectById(64);
				if (bankDoor[0] != -1) {
					atObject(bankDoor[1], bankDoor[2]);
					wait(random(1000, 1200));
				} else {
					walkToWait(150, 504);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doSmithing(int barID, int itemID, String item) {
		try {
			handleEvents();
			int barsNeeded = 0;
			if (questMenuOpen() && !isSleeping()) {
				if (item.equals("Dagger")) {
					barsNeeded = 1;
					answerQuestMenuStringContains("Make Weapon", false);
					answerQuestMenuStringContains("Dagger", true);
				}
				if (item.equals("2-handed sword")) {
					barsNeeded = 3;
					answerQuestMenuStringContains("Make Weapon", false);
					answerQuestMenuStringContains("Sword", false);
					answerQuestMenuStringContains("2-handed sword", true);
				}
				if (item.equals("Plate mail Legs")
						|| item.equals("Plated Skirt")) {
					answerQuestMenuStringContains("Make Armour", false);
					answerQuestMenuStringContains("Armour", false);
					answerQuestMenuStringContains(item, true);
				}

				if (item.equals(item.equals("Plate mail body"))) {
					barsNeeded = 5;
					answerQuestMenuStringContains("Make Armour", false);
					answerQuestMenuStringContains("Armour", false);
					answerQuestMenuStringContains(item, true);
				}
				if (item.equals("Large Helmet")) {
					barsNeeded = 2;
					answerQuestMenuStringContains("Make Armour", false);
					answerQuestMenuStringContains("Helmet", false);
					answerQuestMenuStringContains("Large Helmet", true);
				}
				if (item.equals("Kite Shield")) {
					barsNeeded = 3;
					answerQuestMenuStringContains("Make Armour", false);
					answerQuestMenuStringContains("Shield", false);
					answerQuestMenuStringContains("Kite Shield", true);
				}
			} else {
				if (ourPlayerY() > 509 && ourPlayerX() < 149) {
					int[] anvil = getObjectById(50);
					if (anvil[0] != -1) {
						if (itemCount(barID) >= barsNeeded && !isSleeping()) {
							handleEvents();
							useItemOnObject(anvil[1], anvil[2], barID);
							wait(random(1100, 1300));
						}
					}
				} else {
					int[] doors = getObjectById(411);
					if (doors[1] != -1) {
						atObject(doors[1], doors[2]);
						wait(random(1000, 1200));
					}
					walkToWait(146, 512);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static int[] checkBarType(String arg) {

		barEnum be = barEnum.valueOf(arg.toLowerCase());
		int[] val = new int[9];

		switch (be) {

		case bronze:
			val[0] = 169; // BarID
			val[1] = 62; // DaggerID
			val[2] = 108; // LargeHelmID
			val[3] = 128; // KiteShieldID
			val[4] = 76; // 2handID
			val[5] = 206; // PlateLegsID
			val[6] = 214; // PlateSkirtID
			val[7] = 113; // PlateMailBodyID
			val[8] = 0;
			break;

		case iron:
			val[0] = 170; // BarID
			val[1] = 28; // DaggerID
			val[2] = 6; // LargeHelmID
			val[3] = 2; // KiteShieldID
			val[4] = 77; // 2handID
			val[5] = 9; // PlateLegsID
			val[6] = 215; // PlateSkirtID
			val[7] = 8; // PlateMailBodyID
			val[8] = 1;
			break;

		case steel:
			val[0] = 171; // BarID
			val[1] = 63; // DaggerID
			val[2] = 109; // LargeHelmID
			val[3] = 129; // KiteShieldID
			val[4] = 78; // 2handID
			val[5] = 121; // PlateLegsID
			val[6] = 225; // PlateSkirtID
			val[7] = 118; // PlateMailBodyID
			val[8] = 2;
			break;

		case mithril:
			val[0] = 173; // BarID
			val[1] = 64; // DaggerID
			val[2] = 110; // LargeHelmID
			val[3] = 130; // KiteShieldID
			val[4] = 79; // 2handID
			val[5] = 122; // PlateLegsID
			val[6] = 226; // PlateSkirtID
			val[7] = 119; // PlateMailBodyID
			val[8] = 3;
			break;

		case adamantite:
			val[0] = 174; // BarID
			val[1] = 65; // DaggerID
			val[2] = 111; // LargeHelmID
			val[3] = 131; // KiteShieldID
			val[4] = 80; // 2handID
			val[5] = 123; // PlateLegsID
			val[6] = 227; // PlateSkirtID
			val[7] = 120; // PlateMailBodyID
			val[8] = 4;
			break;

		case runite:
			val[0] = 408; // BarID
			val[1] = 369; // DaggerID
			val[2] = 112; // LargeHelmID
			val[3] = 404; // KiteShieldID
			val[4] = 81; // 2handID
			val[5] = 402; // PlateLegsID
			val[6] = 406; // PlateSkirtID
			val[7] = 401; // PlateMailBodyID
			val[8] = 5;
			break;

		}
		return val;
	}
}